package com.zzsy.carosshopprojects.activitys.home.mainpage.quickjoinoil;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseApplation;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.SPUtil;
import com.zzsy.carosshopprojects.util.ViewBgUtil;
import com.zzsy.carosshopprojects.util.imageUtil.GlideUtil;
import butterknife.BindView;

/**
 * 快速加油
 */
public class QuickJoinOilFragment  extends BaseFragment {
    public static QuickJoinOilFragment newInstance() {
        QuickJoinOilFragment quickJoinOilFragment = new QuickJoinOilFragment();
        Bundle bundle = new Bundle();
        quickJoinOilFragment.setArguments(bundle);
        return quickJoinOilFragment;
    }

    @BindView(R.id.img_qrcode)
    ImageView imgQRCode;
    @BindView(R.id.goto_home)
    TextView tvGotoHome;
    @BindView(R.id.tv_deviceMsg)
    TextView tv_DeviceMsg;

    @BindView(R.id.ly_bg)
    LinearLayout ly_bg;

    @Override
    protected int initLayout() {
        return R.layout.fg_quickjoinoil;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.ShowCode), getActivity());
        ly_bg.setBackground(ViewBgUtil.setWidthBg(15, 1, Color.parseColor("#FFFFFF"), Color.parseColor("#707070")));
        tvGotoHome.setOnClickListener(view1 -> {
            getActivity().finish();
        });
    }

    @Override
    protected void initData(Context mContext) {
        tv_DeviceMsg.setText(SPUtil.getInstance().getString(ConfigureFile.NAME, ""));
    }

    @Override
    protected void loadMethod() {
        getQR();
    }

    private void getQR() {
        String sceneStr = "quick_" + DataUtil.getInstance().getStoreId() + "_" + DataUtil.getInstance().getTerminalId();
        RequestUtils.GeQuickJoinOilQr(getContext(), sceneStr, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean baseBean) {

             }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    GlideUtil.loadImage(BaseApplation.instance().getApplicationContext(), errorMsg == null ? "" : errorMsg, imgQRCode);
                } else {
                    ToastUtils.showToast(BaseApplation.instance().getApplicationContext(), errorMsg);
                }
            }
        });
    }
}
